import { logger } from '../core/logger';
import { syncInfo } from '../models/syncInfo';
import { clientBase, HOST } from './clientBase';
import { ajaxResult } from '../models/ajaxResult';
import { uploadInfo } from '../models/uploadInfo';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class syncClient extends clientBase {
  uploadData(data: uploadInfo[]): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/sync/uploadData`, data, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  getData(areaCode: string, deviceId: string = ""): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/sync/getData?areaCode=${areaCode}&deviceId=${deviceId}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  irrigateNow(areaCode: string, deviceId: string = ""): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      let info = new syncInfo();
      info.areaCode = areaCode;
      info.deviceId = deviceId;
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/sync/irrigateNow`, info, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  irrigateStop(areaCode: string, deviceId: string = ""): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      let info = new syncInfo();
      info.areaCode = areaCode;
      info.deviceId = deviceId;
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/sync/irrigateStop`, info, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  irrigateInterval(areaCode: string, interval: number, deviceId: string = ""): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      const info: syncInfo = {
        areaCode: areaCode,
        deviceId: deviceId,
        threshold: 0,
        interval: interval
      };
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/sync/irrigateInterval`, info, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  irrigateThreshold(areaCode: string, threshold: number, deviceId: string = ""): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      let info: syncInfo = {
        areaCode: areaCode,
        deviceId: deviceId,
        threshold: threshold,
        interval: 0
      };
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/sync/irrigateThreshold`, info, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }
}
export default new syncClient();